/*
 * @(#)ServiceActionException.java
 * Date 2013-08-30
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.exception;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.ClassUtils;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-08-30
 * @author         Jim Horner
 */
public class ServiceActionException extends BaseException {

    /** Field description */
    private final List<ServiceActionError> errors;

    /** Field description */
    private final String location;

    /**
     * Constructs ...
     *
     *
     * @param location
     * @param errors
     */
    public ServiceActionException(Class location, Collection<ServiceActionError> errors) {

        this(ClassUtils.getShortClassName(location), errors);
    }

    /**
     * Constructs ...
     *
     *
     * @param location
     * @param error
     */
    public ServiceActionException(Class location, ServiceActionError error) {

        this(location, Collections.singletonList(error));
    }

    /**
     *
     */
    public ServiceActionException(String location,
                                  Collection<ServiceActionError> errors) {

        super(createMessage(errors));
        this.location = location;
        this.errors = new ArrayList<>();
        this.errors.addAll(errors);
    }

    /**
     * Constructs ...
     *
     *
     * @param location
     * @param error
     */
    public ServiceActionException(String location, ServiceActionError error) {

        this(location, Collections.singletonList(error));
    }

    /**
     * Constructs ...
     *
     *
     * @param cause
     * @param location
     * @param errors
     */
    public ServiceActionException(Throwable cause, Class location,
                                  Collection<ServiceActionError> errors) {

        this(cause, ClassUtils.getShortClassName(location), errors);
    }

    /**
     * Constructs ...
     *
     *
     * @param cause
     * @param location
     * @param error
     */
    public ServiceActionException(Throwable cause, Class location,
                                  ServiceActionError error) {

        this(cause, location, Collections.singletonList(error));
    }

    /**
     * Constructs ...
     *
     *
     * @param cause
     */
    public ServiceActionException(Throwable cause, String location,
                                  Collection<ServiceActionError> errors) {

        super(createMessage(errors), cause);
        this.location = location;
        this.errors = new ArrayList<>();
        this.errors.addAll(errors);
    }

    /**
     * Constructs ...
     *
     *
     * @param cause
     * @param location
     * @param error
     */
    public ServiceActionException(Throwable cause, String location,
                                  ServiceActionError error) {

        this(cause, location, Collections.singletonList(error));
    }

    /**
     * Method description
     *
     *
     * @param errors
     *
     * @return
     */
    private static String createMessage(Collection<ServiceActionError> errors) {

        StringBuilder result = new StringBuilder();

        for (ServiceActionError error : errors) {

            if (result.length() > 0) {
                result.append("\n");
            }

            result.append(error.getCodeContext());
        }

        return result.toString();
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public ServiceActionError[] getErrors() {

        return this.errors.toArray(new ServiceActionError[this.errors.size()]);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public String getLocation() {

        return this.location;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public boolean hasErrors() {

        return (this.errors.isEmpty() == false);
    }
}
